package hut.ai.team.actor;

import hut.ai.team.object.Chot;
import hut.ai.team.object.Ma;
import hut.ai.team.object.Phao;
import hut.ai.team.object.QuanCo;
import hut.ai.team.object.Sy;
import hut.ai.team.object.Tinh;
import hut.ai.team.object.Tuong;
import hut.ai.team.object.Xe;
import hut.ai.team.screen.MainScreen;
import hut.ai.team.table.NuocDi;
import hut.ai.team.table.VanCo;

import java.util.ArrayList;

public class Gen {

	public  ArrayList<NuocDi> nextMove = new ArrayList<NuocDi>();
	public  NuocDi temp;

	public  ArrayList<NuocDi> SinhNuocDi() {
		nextMove = new ArrayList<NuocDi>();
		if (MainScreen.getInstance().computer.Phe == VanCo.LuotDi){
			SinhNuocDiChoAI();
		}
		else{
			SinhNuocChoNguoiChoi();
		}
		return nextMove;
	}
	
	// SinhNuocDiChoAI va SinhNuocChoNguoiChoi
	public  ArrayList<NuocDi> SinhNuocDiChoAI() {
		nextMove.clear();
		for (int k = 0; k < MainScreen.getInstance().computer.dsQuanCo.size(); k++) {
			if(MainScreen.getInstance().computer.dsQuanCo.get(k).TrangThai != 0){
				for (int i = 0; i <= 9; i++) {
					for (int j = 0; j <= 8; j++) {
						KiemTraAI(i, j, MainScreen.getInstance().computer.dsQuanCo.get(k));
					}
				}
			}
		}
		return nextMove;
	}

	public  ArrayList<NuocDi> SinhNuocChoNguoiChoi() {
		nextMove.clear();
		for (int k = 0; k < MainScreen.getInstance().n1.dsQuanCo.size(); k++) {
			if(MainScreen.getInstance().n1.dsQuanCo.get(k).TrangThai != 0){
				for (int i = 0; i <= 9; i++) {
					for (int j = 0; j <= 8; j++) {
						KiemTra(i, j, MainScreen.getInstance().n1.dsQuanCo.get(k));
					}
				}
			}
		}
		return nextMove;
	}

	public void KiemTraAI(int i, int j, QuanCo q){
		if(q.TrangThai == 1){
			if (i != q.Hang	|| j != q.Cot) {
				switch (q.LoaiCL) {
				case 2:
					if(Sy.KiemTra(q, i, j) == 1 && Sy.TuongAnToan(q, i, j) == 1){
						NuocDi next = new NuocDi();
						next.Cuoi.Hang = i;
						next.Cuoi.Cot = j;
						next.Dau = q;
						if (MainScreen.getInstance().mBanCo.ViTri[next.Cuoi.Hang][next.Cuoi.Cot].Trong == false) {
							for (int l = 0; l < MainScreen.getInstance().n1.dsQuanCo.size(); l++) {
								if (next.Cuoi.Hang == MainScreen.getInstance().n1.dsQuanCo.get(l).Hang
										&& next.Cuoi.Cot == MainScreen.getInstance().n1.dsQuanCo.get(l).Cot) {
									next.Cuoi = MainScreen.getInstance().n1.dsQuanCo.get(l);
									break;
								}
							}
						}
						nextMove.add(next);
					}
					break;
				case 3:
					if(Tinh.KiemTra(q, i, j) == 1 && Tinh.TuongAnToan(q, i, j) == 1){
						NuocDi next = new NuocDi();
						next.Cuoi.Hang = i;
						next.Cuoi.Cot = j;
						next.Dau = q;
						if (MainScreen.getInstance().mBanCo.ViTri[next.Cuoi.Hang][next.Cuoi.Cot].Trong == false) {
							for (int l = 0; l < MainScreen.getInstance().n1.dsQuanCo.size(); l++) {
								if (next.Cuoi.Hang == MainScreen.getInstance().n1.dsQuanCo.get(l).Hang
										&& next.Cuoi.Cot == MainScreen.getInstance().n1.dsQuanCo.get(l).Cot) {
									next.Cuoi = MainScreen.getInstance().n1.dsQuanCo.get(l);
									break;
								}
							}
						}
						nextMove.add(next);
					}		
					break;
				case 4:
					if(Xe.KiemTra(q, i, j) == 1 && Xe.TuongAnToan(q, i, j) == 1){
						NuocDi next = new NuocDi();
						next.Cuoi.Hang = i;
						next.Cuoi.Cot = j;
						next.Dau = q;
						if (MainScreen.getInstance().mBanCo.ViTri[next.Cuoi.Hang][next.Cuoi.Cot].Trong == false) {
							for (int l = 0; l < MainScreen.getInstance().n1.dsQuanCo.size(); l++) {
								if (next.Cuoi.Hang == MainScreen.getInstance().n1.dsQuanCo.get(l).Hang
										&& next.Cuoi.Cot == MainScreen.getInstance().n1.dsQuanCo.get(l).Cot) {
									next.Cuoi = MainScreen.getInstance().n1.dsQuanCo.get(l);
									break;
								}
							}
						}
						nextMove.add(next);
					}
					break;
				case 5:
					if(Phao.KiemTra(q, i, j) == 1 && Phao.TuongAnToan(q, i, j) == 1){
						NuocDi next = new NuocDi();
						next.Cuoi.Hang = i;
						next.Cuoi.Cot = j;
						next.Dau = q;
						if (MainScreen.getInstance().mBanCo.ViTri[next.Cuoi.Hang][next.Cuoi.Cot].Trong == false) {
							for (int l = 0; l < MainScreen.getInstance().n1.dsQuanCo.size(); l++) {
								if (next.Cuoi.Hang == MainScreen.getInstance().n1.dsQuanCo.get(l).Hang
										&& next.Cuoi.Cot == MainScreen.getInstance().n1.dsQuanCo.get(l).Cot) {
									next.Cuoi = MainScreen.getInstance().n1.dsQuanCo.get(l);
									break;
								}
							}
						}
						nextMove.add(next);
					}
					break;
				case 6:
					if(Ma.KiemTra(q, i, j) == 1 && Ma.TuongAnToan(q, i, j) == 1){
						NuocDi next = new NuocDi();
						next.Cuoi.Hang = i;
						next.Cuoi.Cot = j;
						next.Dau = q;
						if (MainScreen.getInstance().mBanCo.ViTri[next.Cuoi.Hang][next.Cuoi.Cot].Trong == false) {
							for (int l = 0; l < MainScreen.getInstance().n1.dsQuanCo.size(); l++) {
								if (next.Cuoi.Hang == MainScreen.getInstance().n1.dsQuanCo.get(l).Hang
										&& next.Cuoi.Cot == MainScreen.getInstance().n1.dsQuanCo.get(l).Cot) {
									next.Cuoi = MainScreen.getInstance().n1.dsQuanCo.get(l);
									break;
								}
							}
						}
						nextMove.add(next);
					}
					break;
				case 7:
					if(Chot.KiemTra(q, i, j) == 1 && Chot.TuongAnToan(q, i, j) == 1){
						NuocDi next = new NuocDi();
						next.Cuoi.Hang = i;
						next.Cuoi.Cot = j;
						next.Dau = q;
						if (MainScreen.getInstance().mBanCo.ViTri[next.Cuoi.Hang][next.Cuoi.Cot].Trong == false) {
							for (int l = 0; l < MainScreen.getInstance().n1.dsQuanCo.size(); l++) {
								if (next.Cuoi.Hang == MainScreen.getInstance().n1.dsQuanCo.get(l).Hang
										&& next.Cuoi.Cot == MainScreen.getInstance().n1.dsQuanCo.get(l).Cot) {
									next.Cuoi = MainScreen.getInstance().n1.dsQuanCo.get(l);
									break;
								}
							}
						}
						nextMove.add(next);
					}
					break;
				default:
					break;
				}
			}
		}else{
			if (i != q.Hang || j != q.Cot) {
				switch (q.Loai) {
				case 1:
					if(Tuong.KiemTra(q, i, j) == 1 && Tuong.TuongAnToan(q, i, j) == 1){
						NuocDi next = new NuocDi();
						next.Cuoi.Hang = i;
						next.Cuoi.Cot = j;
						next.Dau = q;
						if (MainScreen.getInstance().mBanCo.ViTri[next.Cuoi.Hang][next.Cuoi.Cot].Trong == false) {
							for (int l = 0; l < MainScreen.getInstance().n1.dsQuanCo.size(); l++) {
								if (next.Cuoi.Hang == MainScreen.getInstance().n1.dsQuanCo.get(l).Hang
										&& next.Cuoi.Cot == MainScreen.getInstance().n1.dsQuanCo.get(l).Cot) {
									next.Cuoi = MainScreen.getInstance().n1.dsQuanCo.get(l);
									break;
								}
							}
						}
						nextMove.add(next);
					}
					break;
				case 2:
					if(Sy.KiemTra(q, i, j) == 1 && Sy.TuongAnToan(q, i, j) == 1){
						NuocDi next = new NuocDi();
						next.Cuoi.Hang = i;
						next.Cuoi.Cot = j;
						next.Dau = q;
						if (MainScreen.getInstance().mBanCo.ViTri[next.Cuoi.Hang][next.Cuoi.Cot].Trong == false) {
							for (int l = 0; l < MainScreen.getInstance().n1.dsQuanCo.size(); l++) {
								if (next.Cuoi.Hang == MainScreen.getInstance().n1.dsQuanCo.get(l).Hang
										&& next.Cuoi.Cot == MainScreen.getInstance().n1.dsQuanCo.get(l).Cot) {
									next.Cuoi = MainScreen.getInstance().n1.dsQuanCo.get(l);
									break;
								}
							}
						}
						nextMove.add(next);
					}
					break;
				case 3:
					if(Tinh.KiemTra(q, i, j) == 1 && Tinh.TuongAnToan(q, i, j) == 1){
						NuocDi next = new NuocDi();
						next.Cuoi.Hang = i;
						next.Cuoi.Cot = j;
						next.Dau = q;
						if (MainScreen.getInstance().mBanCo.ViTri[next.Cuoi.Hang][next.Cuoi.Cot].Trong == false) {
							for (int l = 0; l < MainScreen.getInstance().n1.dsQuanCo.size(); l++) {
								if (next.Cuoi.Hang == MainScreen.getInstance().n1.dsQuanCo.get(l).Hang
										&& next.Cuoi.Cot == MainScreen.getInstance().n1.dsQuanCo.get(l).Cot) {
									next.Cuoi = MainScreen.getInstance().n1.dsQuanCo.get(l);
									break;
								}
							}
						}
						nextMove.add(next);
					}		
					break;
				case 4:
					if(Xe.KiemTra(q, i, j) == 1 && Xe.TuongAnToan(q, i, j) == 1){
						NuocDi next = new NuocDi();
						next.Cuoi.Hang = i;
						next.Cuoi.Cot = j;
						next.Dau = q;
						if (MainScreen.getInstance().mBanCo.ViTri[next.Cuoi.Hang][next.Cuoi.Cot].Trong == false) {
							for (int l = 0; l < MainScreen.getInstance().n1.dsQuanCo.size(); l++) {
								if (next.Cuoi.Hang == MainScreen.getInstance().n1.dsQuanCo.get(l).Hang
										&& next.Cuoi.Cot == MainScreen.getInstance().n1.dsQuanCo.get(l).Cot) {
									next.Cuoi = MainScreen.getInstance().n1.dsQuanCo.get(l);
									break;
								}
							}
						}
						nextMove.add(next);
					}
					break;
				case 5:
					if(Phao.KiemTra(q, i, j) == 1 && Phao.TuongAnToan(q, i, j) == 1){
						NuocDi next = new NuocDi();
						next.Cuoi.Hang = i;
						next.Cuoi.Cot = j;
						next.Dau = q;
						if (MainScreen.getInstance().mBanCo.ViTri[next.Cuoi.Hang][next.Cuoi.Cot].Trong == false) {
							for (int l = 0; l < MainScreen.getInstance().n1.dsQuanCo.size(); l++) {
								if (next.Cuoi.Hang == MainScreen.getInstance().n1.dsQuanCo.get(l).Hang
										&& next.Cuoi.Cot == MainScreen.getInstance().n1.dsQuanCo.get(l).Cot) {
									next.Cuoi = MainScreen.getInstance().n1.dsQuanCo.get(l);
									break;
								}
							}
						}
						nextMove.add(next);
					}
					break;
				case 6:
					if(Ma.KiemTra(q, i, j) == 1 && Ma.TuongAnToan(q, i, j) == 1){
						NuocDi next = new NuocDi();
						next.Cuoi.Hang = i;
						next.Cuoi.Cot = j;
						next.Dau = q;
						if (MainScreen.getInstance().mBanCo.ViTri[next.Cuoi.Hang][next.Cuoi.Cot].Trong == false) {
							for (int l = 0; l < MainScreen.getInstance().n1.dsQuanCo.size(); l++) {
								if (next.Cuoi.Hang == MainScreen.getInstance().n1.dsQuanCo.get(l).Hang
										&& next.Cuoi.Cot == MainScreen.getInstance().n1.dsQuanCo.get(l).Cot) {
									next.Cuoi = MainScreen.getInstance().n1.dsQuanCo.get(l);
									break;
								}
							}
						}
						nextMove.add(next);
					}
					break;
				case 7:
					if(Chot.KiemTra(q, i, j) == 1 && Chot.TuongAnToan(q, i, j) == 1){
						NuocDi next = new NuocDi();
						next.Cuoi.Hang = i;
						next.Cuoi.Cot = j;
						next.Dau = q;
						if (MainScreen.getInstance().mBanCo.ViTri[next.Cuoi.Hang][next.Cuoi.Cot].Trong == false) {
							for (int l = 0; l < MainScreen.getInstance().n1.dsQuanCo.size(); l++) {
								if (next.Cuoi.Hang == MainScreen.getInstance().n1.dsQuanCo.get(l).Hang
										&& next.Cuoi.Cot == MainScreen.getInstance().n1.dsQuanCo.get(l).Cot) {
									next.Cuoi = MainScreen.getInstance().n1.dsQuanCo.get(l);
									break;
								}
							}
						}
						nextMove.add(next);
					}
					break;
				default:
					break;
				}
			}
		}
	}
	
	public void KiemTra(int i, int j, QuanCo q) {
		if(q.TrangThai == 1){
			if (i != q.Hang	|| j != q.Cot) {
				switch (q.LoaiCL) {
				case 2:
					if(Sy.KiemTra(q, i, j) == 1 && Sy.TuongAnToan(q, i, j) == 1){
						NuocDi next = new NuocDi();
						next.Cuoi.Hang = i;
						next.Cuoi.Cot = j;
						next.Dau = q;
						if (MainScreen.getInstance().mBanCo.ViTri[next.Cuoi.Hang][next.Cuoi.Cot].Trong == false) {
							for (int l = 0; l < MainScreen.getInstance().computer.dsQuanCo.size(); l++) {
								if (next.Cuoi.Hang == MainScreen.getInstance().computer.dsQuanCo.get(l).Hang
										&& next.Cuoi.Cot == MainScreen.getInstance().computer.dsQuanCo.get(l).Cot) {
									next.Cuoi = MainScreen.getInstance().computer.dsQuanCo.get(l);
									break;
								}
							}
						}
						nextMove.add(next);
					}
					break;
				case 3:
					if(Tinh.KiemTra(q, i, j) == 1 && Tinh.TuongAnToan(q, i, j) == 1){
						NuocDi next = new NuocDi();
						next.Cuoi.Hang = i;
						next.Cuoi.Cot = j;
						next.Dau = q;
						if (MainScreen.getInstance().mBanCo.ViTri[next.Cuoi.Hang][next.Cuoi.Cot].Trong == false) {
							for (int l = 0; l < MainScreen.getInstance().computer.dsQuanCo.size(); l++) {
								if (next.Cuoi.Hang == MainScreen.getInstance().computer.dsQuanCo.get(l).Hang
										&& next.Cuoi.Cot == MainScreen.getInstance().computer.dsQuanCo.get(l).Cot) {
									next.Cuoi = MainScreen.getInstance().computer.dsQuanCo.get(l);
									break;
								}
							}
						}
						nextMove.add(next);
					}		
					break;
				case 4:
					if(Xe.KiemTra(q, i, j) == 1 && Xe.TuongAnToan(q, i, j) == 1){
						NuocDi next = new NuocDi();
						next.Cuoi.Hang = i;
						next.Cuoi.Cot = j;
						next.Dau = q;
						if (MainScreen.getInstance().mBanCo.ViTri[next.Cuoi.Hang][next.Cuoi.Cot].Trong == false) {
							for (int l = 0; l < MainScreen.getInstance().computer.dsQuanCo.size(); l++) {
								if (next.Cuoi.Hang == MainScreen.getInstance().computer.dsQuanCo.get(l).Hang
										&& next.Cuoi.Cot == MainScreen.getInstance().computer.dsQuanCo.get(l).Cot) {
									next.Cuoi = MainScreen.getInstance().computer.dsQuanCo.get(l);
									break;
								}
							}
						}
						nextMove.add(next);
					}
					break;
				case 5:
					if(Phao.KiemTra(q, i, j) == 1 && Phao.TuongAnToan(q, i, j) == 1){
						NuocDi next = new NuocDi();
						next.Cuoi.Hang = i;
						next.Cuoi.Cot = j;
						next.Dau = q;
						if (MainScreen.getInstance().mBanCo.ViTri[next.Cuoi.Hang][next.Cuoi.Cot].Trong == false) {
							for (int l = 0; l < MainScreen.getInstance().computer.dsQuanCo.size(); l++) {
								if (next.Cuoi.Hang == MainScreen.getInstance().computer.dsQuanCo.get(l).Hang
										&& next.Cuoi.Cot == MainScreen.getInstance().computer.dsQuanCo.get(l).Cot) {
									next.Cuoi = MainScreen.getInstance().computer.dsQuanCo.get(l);
									break;
								}
							}
						}
						nextMove.add(next);
					}
					break;
				case 6:
					if(Ma.KiemTra(q, i, j) == 1 && Ma.TuongAnToan(q, i, j) == 1){
						NuocDi next = new NuocDi();
						next.Cuoi.Hang = i;
						next.Cuoi.Cot = j;
						next.Dau = q;
						if (MainScreen.getInstance().mBanCo.ViTri[next.Cuoi.Hang][next.Cuoi.Cot].Trong == false) {
							for (int l = 0; l < MainScreen.getInstance().computer.dsQuanCo.size(); l++) {
								if (next.Cuoi.Hang == MainScreen.getInstance().computer.dsQuanCo.get(l).Hang
										&& next.Cuoi.Cot == MainScreen.getInstance().computer.dsQuanCo.get(l).Cot) {
									next.Cuoi = MainScreen.getInstance().computer.dsQuanCo.get(l);
									break;
								}
							}
						}
						nextMove.add(next);
					}
					break;
				case 7:
					if(Chot.KiemTra(q, i, j) == 1 && Chot.TuongAnToan(q, i, j) == 1){
						NuocDi next = new NuocDi();
						next.Cuoi.Hang = i;
						next.Cuoi.Cot = j;
						next.Dau = q;
						if (MainScreen.getInstance().mBanCo.ViTri[next.Cuoi.Hang][next.Cuoi.Cot].Trong == false) {
							for (int l = 0; l < MainScreen.getInstance().computer.dsQuanCo.size(); l++) {
								if (next.Cuoi.Hang == MainScreen.getInstance().computer.dsQuanCo.get(l).Hang
										&& next.Cuoi.Cot == MainScreen.getInstance().computer.dsQuanCo.get(l).Cot) {
									next.Cuoi = MainScreen.getInstance().computer.dsQuanCo.get(l);
									break;
								}
							}
						}
						nextMove.add(next);
					}
					break;
				default:
					break;
				}
			}
		}else{
			if (i != q.Hang || j != q.Cot) {
				switch (q.Loai) {
				case 1:
					if(Tuong.KiemTra(q, i, j) == 1 && Tuong.TuongAnToan(q, i, j) == 1){
						NuocDi next = new NuocDi();
						next.Cuoi.Hang = i;
						next.Cuoi.Cot = j;
						next.Dau = q;
						if (MainScreen.getInstance().mBanCo.ViTri[next.Cuoi.Hang][next.Cuoi.Cot].Trong == false) {
							for (int l = 0; l < MainScreen.getInstance().computer.dsQuanCo.size(); l++) {
								if (next.Cuoi.Hang == MainScreen.getInstance().computer.dsQuanCo.get(l).Hang
										&& next.Cuoi.Cot == MainScreen.getInstance().computer.dsQuanCo.get(l).Cot) {
									next.Cuoi = MainScreen.getInstance().computer.dsQuanCo.get(l);
									break;
								}
							}
						}
						nextMove.add(next);
					}
					break;
				case 2:
					if(Sy.KiemTra(q, i, j) == 1 && Sy.TuongAnToan(q, i, j) == 1){
						NuocDi next = new NuocDi();
						next.Cuoi.Hang = i;
						next.Cuoi.Cot = j;
						next.Dau = q;
						if (MainScreen.getInstance().mBanCo.ViTri[next.Cuoi.Hang][next.Cuoi.Cot].Trong == false) {
							for (int l = 0; l < MainScreen.getInstance().computer.dsQuanCo.size(); l++) {
								if (next.Cuoi.Hang == MainScreen.getInstance().computer.dsQuanCo.get(l).Hang
										&& next.Cuoi.Cot == MainScreen.getInstance().computer.dsQuanCo.get(l).Cot) {
									next.Cuoi = MainScreen.getInstance().computer.dsQuanCo.get(l);
									break;
								}
							}
						}
						nextMove.add(next);
					}
					break;
				case 3:
					if(Tinh.KiemTra(q, i, j) == 1 && Tinh.TuongAnToan(q, i, j) == 1){
						NuocDi next = new NuocDi();
						next.Cuoi.Hang = i;
						next.Cuoi.Cot = j;
						next.Dau = q;
						if (MainScreen.getInstance().mBanCo.ViTri[next.Cuoi.Hang][next.Cuoi.Cot].Trong == false) {
							for (int l = 0; l < MainScreen.getInstance().computer.dsQuanCo.size(); l++) {
								if (next.Cuoi.Hang == MainScreen.getInstance().computer.dsQuanCo.get(l).Hang
										&& next.Cuoi.Cot == MainScreen.getInstance().computer.dsQuanCo.get(l).Cot) {
									next.Cuoi = MainScreen.getInstance().computer.dsQuanCo.get(l);
									break;
								}
							}
						}
						nextMove.add(next);
					}		
					break;
				case 4:
					if(Xe.KiemTra(q, i, j) == 1 && Xe.TuongAnToan(q, i, j) == 1){
						NuocDi next = new NuocDi();
						next.Cuoi.Hang = i;
						next.Cuoi.Cot = j;
						next.Dau = q;
						if (MainScreen.getInstance().mBanCo.ViTri[next.Cuoi.Hang][next.Cuoi.Cot].Trong == false) {
							for (int l = 0; l < MainScreen.getInstance().computer.dsQuanCo.size(); l++) {
								if (next.Cuoi.Hang == MainScreen.getInstance().computer.dsQuanCo.get(l).Hang
										&& next.Cuoi.Cot == MainScreen.getInstance().computer.dsQuanCo.get(l).Cot) {
									next.Cuoi = MainScreen.getInstance().computer.dsQuanCo.get(l);
									break;
								}
							}
						}
						nextMove.add(next);
					}
					break;
				case 5:
					if(Phao.KiemTra(q, i, j) == 1 && Phao.TuongAnToan(q, i, j) == 1){
						NuocDi next = new NuocDi();
						next.Cuoi.Hang = i;
						next.Cuoi.Cot = j;
						next.Dau = q;
						if (MainScreen.getInstance().mBanCo.ViTri[next.Cuoi.Hang][next.Cuoi.Cot].Trong == false) {
							for (int l = 0; l < MainScreen.getInstance().computer.dsQuanCo.size(); l++) {
								if (next.Cuoi.Hang == MainScreen.getInstance().computer.dsQuanCo.get(l).Hang
										&& next.Cuoi.Cot == MainScreen.getInstance().computer.dsQuanCo.get(l).Cot) {
									next.Cuoi = MainScreen.getInstance().computer.dsQuanCo.get(l);
									break;
								}
							}
						}
						nextMove.add(next);
					}
					break;
				case 6:
					if(Ma.KiemTra(q, i, j) == 1 && Ma.TuongAnToan(q, i, j) == 1){
						NuocDi next = new NuocDi();
						next.Cuoi.Hang = i;
						next.Cuoi.Cot = j;
						next.Dau = q;
						if (MainScreen.getInstance().mBanCo.ViTri[next.Cuoi.Hang][next.Cuoi.Cot].Trong == false) {
							for (int l = 0; l < MainScreen.getInstance().computer.dsQuanCo.size(); l++) {
								if (next.Cuoi.Hang == MainScreen.getInstance().computer.dsQuanCo.get(l).Hang
										&& next.Cuoi.Cot == MainScreen.getInstance().computer.dsQuanCo.get(l).Cot) {
									next.Cuoi = MainScreen.getInstance().computer.dsQuanCo.get(l);
									break;
								}
							}
						}
						nextMove.add(next);
					}
					break;
				case 7:
					if(Chot.KiemTra(q, i, j) == 1 && Chot.TuongAnToan(q, i, j) == 1){
						NuocDi next = new NuocDi();
						next.Cuoi.Hang = i;
						next.Cuoi.Cot = j;
						next.Dau = q;
						if (MainScreen.getInstance().mBanCo.ViTri[next.Cuoi.Hang][next.Cuoi.Cot].Trong == false) {
							for (int l = 0; l < MainScreen.getInstance().computer.dsQuanCo.size(); l++) {
								if (next.Cuoi.Hang == MainScreen.getInstance().computer.dsQuanCo.get(l).Hang
										&& next.Cuoi.Cot == MainScreen.getInstance().computer.dsQuanCo.get(l).Cot) {
									next.Cuoi = MainScreen.getInstance().computer.dsQuanCo.get(l);
									break;
								}
							}
						}
						nextMove.add(next);
					}
					break;
				default:
					break;
				}
			}
		}
	}
	
	// MakeMove va UnMakeMove
	public void UnMakeMove(NuocDi nuocDi) {
		if (VanCo.LuotDi == 1)
			VanCo.LuotDi = 2;
		else
			VanCo.LuotDi = 1;
		temp = nuocDi;
		
		// Tra ve o dau
		MainScreen.getInstance().mBanCo.ViTri[temp.Dau.Hang][temp.Dau.Cot].Trong = false;
		MainScreen.getInstance().mBanCo.ViTri[temp.Dau.Hang][temp.Dau.Cot].Phe = temp.Dau.Phe;
		MainScreen.getInstance().mBanCo.ViTri[temp.Dau.Hang][temp.Dau.Cot].TrangThai = temp.Dau.TrangThai;
		if(nuocDi.Dau.TrangThai == 1)
			MainScreen.getInstance().mBanCo.ViTri[temp.Dau.Hang][temp.Dau.Cot].Loai = temp.Dau.LoaiCL;
		else
			MainScreen.getInstance().mBanCo.ViTri[temp.Dau.Hang][temp.Dau.Cot].Loai = temp.Dau.Loai;
//		System.out.println("dau [" + MainScreen.getInstance().mBanCo.ViTri[temp.Dau.Hang][temp.Dau.Cot].Phe + "," + MainScreen.getInstance().mBanCo.ViTri[temp.Dau.Hang][temp.Dau.Cot].TrangThai + "," + MainScreen.getInstance().mBanCo.ViTri[temp.Dau.Hang][temp.Dau.Cot].Loai + "]");
		if (temp.Dau.Phe == MainScreen.getInstance().computer.Phe) {
			// tra ve hang dau, cot dau cho
			for (int i = 0; i < MainScreen.getInstance().computer.dsQuanCo.size(); i++) {
				if (temp.Cuoi.Hang == MainScreen.getInstance().computer.dsQuanCo.get(i).Hang 
						&& temp.Cuoi.Cot == MainScreen.getInstance().computer.dsQuanCo.get(i).Cot) {
					MainScreen.getInstance().computer.dsQuanCo.get(i).Hang = temp.Dau.Hang;
					MainScreen.getInstance().computer.dsQuanCo.get(i).Cot = temp.Dau.Cot;
					break;
				}
			}
		}else {
			for (int i = 0; i < MainScreen.getInstance().n1.dsQuanCo.size(); i++) {
				if (temp.Cuoi.Hang == MainScreen.getInstance().n1.dsQuanCo.get(i).Hang 
						&& temp.Cuoi.Cot == MainScreen.getInstance().n1.dsQuanCo.get(i).Cot) {
					MainScreen.getInstance().n1.dsQuanCo.get(i).Hang = temp.Dau.Hang;
					MainScreen.getInstance().n1.dsQuanCo.get(i).Cot = temp.Dau.Cot;
					break;
				}
			}
		}
		// Tra ve o cuoi
		if(temp.Cuoi.TrangThai != 0){
			System.out.println("Tra ve quan co cuoi");
			MainScreen.getInstance().mBanCo.ViTri[temp.Cuoi.Hang][temp.Cuoi.Cot].Trong = false;
			MainScreen.getInstance().mBanCo.ViTri[temp.Cuoi.Hang][temp.Cuoi.Cot].Phe = temp.Cuoi.Phe;
			MainScreen.getInstance().mBanCo.ViTri[temp.Cuoi.Hang][temp.Cuoi.Cot].TrangThai = temp.Cuoi.TrangThai;
			if(temp.Cuoi.TrangThai == 1)
				MainScreen.getInstance().mBanCo.ViTri[temp.Cuoi.Hang][temp.Cuoi.Cot].Loai = temp.Cuoi.LoaiCL;
			else
				MainScreen.getInstance().mBanCo.ViTri[temp.Cuoi.Hang][temp.Cuoi.Cot].Loai = temp.Cuoi.Loai;
			if (temp.Cuoi.Phe == MainScreen.getInstance().computer.Phe) {
				for (int i = 0; i < MainScreen.getInstance().computer.dsQuanCo.size(); i++) {
					if (temp.Cuoi.Hang == MainScreen.getInstance().computer.dsQuanCo.get(i).Hang 
							&& temp.Cuoi.Cot == MainScreen.getInstance().computer.dsQuanCo.get(i).Cot) {
						MainScreen.getInstance().computer.dsQuanCo.get(i).TrangThai = temp.Cuoi.TrangThai;
						break;
					}
				}
			}else {
				for (int i = 0; i < MainScreen.getInstance().n1.dsQuanCo.size(); i++) {
					if (temp.Cuoi.Hang == MainScreen.getInstance().n1.dsQuanCo.get(i).Hang 
							&& temp.Cuoi.Cot == MainScreen.getInstance().n1.dsQuanCo.get(i).Cot) {
						MainScreen.getInstance().computer.dsQuanCo.get(i).TrangThai = temp.Cuoi.TrangThai;
						break;
					}
				}
			}
		}else{
			MainScreen.getInstance().mBanCo.OCoTrong(nuocDi.Cuoi.Hang, nuocDi.Cuoi.Cot);
		}
	}

	public boolean MakeMove(NuocDi nuocDi) {
		int hd,cd,ttd,ttc;
		temp = nuocDi;
		hd = temp.Dau.Hang;
		cd = temp.Dau.Cot;
		ttd = temp.Dau.TrangThai;
		ttc = temp.Cuoi.TrangThai;
		
		MainScreen.getInstance().mBanCo.OCoTrong(temp.Dau.Hang, temp.Dau.Cot);
		MainScreen.getInstance().mBanCo.ViTri[temp.Cuoi.Hang][temp.Cuoi.Cot].Trong = false;
		MainScreen.getInstance().mBanCo.ViTri[temp.Cuoi.Hang][temp.Cuoi.Cot].Phe = temp.Dau.Phe;
		MainScreen.getInstance().mBanCo.ViTri[temp.Cuoi.Hang][temp.Cuoi.Cot].TrangThai = 2;
		MainScreen.getInstance().mBanCo.ViTri[temp.Cuoi.Hang][temp.Cuoi.Cot].Loai = temp.Dau.Loai;
		if (temp.Dau.Phe == MainScreen.getInstance().computer.Phe) {
			for (int i = 0; i < MainScreen.getInstance().computer.dsQuanCo.size(); i++) {
				if (temp.Dau.Hang == MainScreen.getInstance().computer.dsQuanCo.get(i).Hang
						&& temp.Dau.Cot == MainScreen.getInstance().computer.dsQuanCo.get(i).Cot) {
					MainScreen.getInstance().computer.dsQuanCo.get(i).Hang = temp.Cuoi.Hang;
					MainScreen.getInstance().computer.dsQuanCo.get(i).Cot = temp.Cuoi.Cot;
					MainScreen.getInstance().computer.dsQuanCo.get(i).TrangThai = 2;
					break;
				}
			}
			if (nuocDi.Cuoi.TrangThai != 0) {
				for (int i = 0; i < MainScreen.getInstance().n1.dsQuanCo.size(); i++) {
					if (nuocDi.Cuoi.Hang == MainScreen.getInstance().n1.dsQuanCo.get(i).Hang && temp.Cuoi.Cot == MainScreen.getInstance().n1.dsQuanCo.get(i).Cot) {
						MainScreen.getInstance().n1.dsQuanCo.get(i).TrangThai = 0;
						break;
					}
				}
			}
		} else {
			for (int i = 0; i < MainScreen.getInstance().n1.dsQuanCo.size(); i++) {
				if (temp.Dau.Hang == MainScreen.getInstance().n1.dsQuanCo.get(i).Hang
						&& temp.Dau.Cot == MainScreen.getInstance().n1.dsQuanCo.get(i).Cot) {
					MainScreen.getInstance().n1.dsQuanCo.get(i).Hang = temp.Cuoi.Hang;
					MainScreen.getInstance().n1.dsQuanCo.get(i).Cot = temp.Cuoi.Cot;
					MainScreen.getInstance().n1.dsQuanCo.get(i).TrangThai = 2;
					break;
				}
			}
			if (temp.Cuoi.TrangThai != 0) {
				for (int i = 0; i < MainScreen.getInstance().computer.dsQuanCo.size(); i++) {
					if (temp.Cuoi.Hang == MainScreen.getInstance().computer.dsQuanCo.get(i).Hang 
							&& temp.Cuoi.Cot == MainScreen.getInstance().computer.dsQuanCo.get(i).Cot) {
						MainScreen.getInstance().computer.dsQuanCo.get(i).TrangThai = 0;
						break;
					}
				}
			}
		}
		temp.Dau.Hang = hd;
		temp.Dau.Cot = cd;
		temp.Dau.TrangThai = ttd;
		temp.Cuoi.TrangThai = ttc;
		Eval ev = new Eval();
		temp.score = Math.abs(ev.UocLuong());
		boolean kill = false;
		if (MainScreen.getInstance().n1 != null && MainScreen.getInstance().computer != null) {
			if (MainScreen.getInstance().n1.dsQuanCo.get(0).TrangThai == 0	|| MainScreen.getInstance().computer.dsQuanCo.get(0).TrangThai == 0)
				kill = true;
			else
				kill = false;
		}
		if (VanCo.LuotDi == 2)
			VanCo.LuotDi = 1;
		else
			VanCo.LuotDi = 2;
		return kill;

	}

}
